#include "MGCCorePlugin.h"

#include "QmlComponentInfo.h"
#include "QmlControls/QmlObjectListModel.h"

class MGCCorePlugin_p
{
public:
    MGCCorePlugin_p()
    {
    }

    ~MGCCorePlugin_p()
    {
        if(pGeneral)
            delete pGeneral;
        if(pCommLinks)
            delete pCommLinks;
        if(pOfflineMaps)
            delete pOfflineMaps;
#if defined(QGC_GST_TAISYNC_ENABLED)
        if(pTaisync)
            delete pTaisync;
#endif
#if defined(QGC_GST_MICROHARD_ENABLED)
        if(pMicrohard)
            delete pMicrohard;
#endif
#if defined(QGC_AIRMAP_ENABLED)
        if(pAirmap)
            delete pAirmap;
#endif
        if(pMAVLink)
            delete pMAVLink;
        if(pConsole)
            delete pConsole;
#if defined(QT_DEBUG)
        if(pMockLink)
            delete pMockLink;
        if(pDebug)
            delete pDebug;
        if(pQmlTest)
            delete pQmlTest;
#endif
        if(defaultOptions)
            delete defaultOptions;
    }

    QmlComponentInfo* pGeneral                  = nullptr;
    QmlComponentInfo* pCommLinks                = nullptr;
    QmlComponentInfo* pOfflineMaps              = nullptr;
#if defined(QGC_GST_TAISYNC_ENABLED)
    QmlComponentInfo* pTaisync                  = nullptr;
#endif
#if defined(QGC_GST_MICROHARD_ENABLED)
    QmlComponentInfo* pMicrohard                = nullptr;
#endif
#if defined(QGC_AIRMAP_ENABLED)
    QmlComponentInfo* pAirmap                   = nullptr;
#endif
    QmlComponentInfo* pMAVLink                  = nullptr;
    QmlComponentInfo* pConsole                  = nullptr;
    QmlComponentInfo* pHelp                     = nullptr;
#if defined(QT_DEBUG)
    QmlComponentInfo* pMockLink                 = nullptr;
    QmlComponentInfo* pDebug                    = nullptr;
    QmlComponentInfo* pQmlTest                  = nullptr;
#endif

    MGCOptions*         defaultOptions          = nullptr;
    QVariantList        settingsList;
    QVariantList        analyzeList;

    QmlObjectListModel _emptyCustomMapItems;
};

MGCCorePlugin::MGCCorePlugin(MGCApplication* app, MGCToolbox* toolbox)
    : MGCTool(app, toolbox)
{
    QQmlEngine::setObjectOwnership(this,QQmlEngine::CppOwnership);
    _p = new MGCCorePlugin_p;
}

MGCCorePlugin::~MGCCorePlugin()
{
    if(_p) {
        delete _p;
    }
}

MGCOptions *MGCCorePlugin::options()
{
    if (!_p->defaultOptions) {
        _p->defaultOptions = new MGCOptions(this);
    }
    return _p->defaultOptions;
}

void MGCCorePlugin::setToolBox(MGCToolbox *toolbox)
{
    MGCTool::setToolbox(toolbox);
    qmlRegisterUncreatableType<MGCCorePlugin>("MGroundControl",1,0,"MGCCorePlugin","Reference only");
}

QQmlApplicationEngine *MGCCorePlugin::createQmlApplicationEngine(QObject *parent)
{
    QQmlApplicationEngine *engine = new QQmlApplicationEngine(parent);
    engine->addImportPath("qrc:/qml");
    return engine;
}

void MGCCorePlugin::createRootWindow(QQmlApplicationEngine *qmlEngine)
{
    qmlEngine->load(QUrl(QStringLiteral("qrc:/qml/MainRootWindow.qml")));
}
